import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import {
  Row,
  Col,
  Card,
  Form,
  Input,
  Button,
  message,
} from 'antd';
import StandardTable from '@/components/StandardTable';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import DescView from './descView';
import styles from '../UserManage.less';
import { powerBtnFunc } from '@/utils/utils';

const FormItem = Form.Item;

/* eslint react/no-multi-comp:0 */
@connect(({ assessment, loading }) => ({
  assessment,
  loading: loading.models.assessment,
}))

@Form.create()
class DeptManage extends PureComponent {
  state = {
    lookDescView: false,
    selectedRows: {},
    formValues: {},
    pagination: {
      pageNo: 1,
      pageSize: 15,
    },
  };


  constructor(props) {
    super(props);
  }

  componentDidMount() {
    const { dispatch } = this.props;
    //请求部门列表
    dispatch({
      type: 'assessment/fetch',
      payload: {},
    });

  }

  handleStandardTableChange = (pagination, filtersArg, sorter) => {
    const { dispatch } = this.props;
    const { formValues } = this.state;


    this.setState({
      pagination: {
        pageNo: pagination.current,
        pageSize: 15,
      },
    });
    const params = {
      pageNo: pagination.current,
      pageSize: pagination.pageSize,
      ...formValues,
    };
    dispatch({
      type: 'assessment/fetch',
      payload: params,
    });
  };
  //重置
  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState({
      formValues: {},
    });
    dispatch({
      type: 'assessment/fetch',
      payload: {},
    });
  };

  //搜索
  handleSearch = e => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;

      const values = {
        ...fieldsValue,
      };
      this.setState({
        formValues: values,
      });

      dispatch({
        type: 'assessment/fetch',
        payload: values,
      });
    });
  };
  //查看
  handleLookDesc = (flag, record) => {
    const { dispatch } = this.props;
    this.setState({
      lookDescView: !!flag,
      selectedRows: record,
    }, () => {
      if (!!flag) {
        dispatch({
          type: 'assessment/detail',
          payload: { userNo: record.userNo },
        });
      }
    });
  };


  renderSimpleForm() {
    const { form: { getFieldDecorator } } = this.props;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
            <FormItem label="手机号码">
              {getFieldDecorator('mobile')(<Input placeholder="请输入手机号"/>)}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="用户编码">
              {getFieldDecorator('userNo')(<Input placeholder="请输入用户编码"/>)}
            </FormItem>
          </Col>
          <Col md={6} sm={24}>
            <div style={{ textAlign: 'center' }}><Button type="primary" htmlType="submit">
              查询
            </Button>
              <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
                重置
              </Button>
            </div>
          </Col>

        </Row>
      </Form>
    );
  }


  render() {
    const { assessment: { details, data }, loading } = this.props;
    const { selectedRows, lookDescView } = this.state;
    const typeList = [
      {
        key: 1,
        value: '保守型',
      },
      {
        key: 2,
        value: '稳健型',
      },
      {
        key: 3,
        value: '积极型',
      },
    ];
    const columns = [
      {
        title: '用户编号',
        dataIndex: 'userNo',
      },

      {
        title: '手机号',
        dataIndex: 'mobile',
      },

      {
        title: '用户名称',
        dataIndex: 'userName',
      },
      {
        title: '类型',
        dataIndex: 'type',
        render: val => <span>{typeList.map((item) => {
          return item.key === val ? item.value : '';
        })}</span>,

      },
      {
        title: '评测时间',
        dataIndex: 'createTime',
      },
      {
        title: '操作',
        render: (text, record) => (
          <Fragment>

            {
              powerBtnFunc('100174',
                <a key={'view'} onClick={() => this.handleLookDesc(true, record)}>查看</a>,
              )}
          </Fragment>
        ),
      },
    ];

    return (
      <PageHeaderWrapper title="风险评测">
        <Card bordered={false}>
          <div className={styles.tableList}>
            <div className={styles.tableListForm}>{this.renderSimpleForm()}</div>

            <StandardTable
              selectedRows={selectedRows}
              loading={loading}
              data={data}
              rowKey={'userNo'}
              columns={columns}
              rowSelectHide={true}

              onChange={this.handleStandardTableChange}
            />
          </div>
        </Card>
        <DescView
          handleLookDesc={this.handleLookDesc}
          selectedRows={details ? details : {}}
          lookDescView={lookDescView}/>

      </PageHeaderWrapper>
    );
  }
}

export default DeptManage;
